//
//  AOrderMallRootVC.m
//  DistributorAB
//
//  Created by LZY on 2019/10/17.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "AOrderMallRootVC.h"

#import "BBuyNowVC.h"//我要预订
#import "ZMyOrderCarVC.h"//预订车
#import "ZMyOrderListVC.h"//预订单
#import "ZMyOrderManagerVC.h"//预订管理
#import "ZGrantListVC.h"//补助金申请

#import "WMZBannerView.h"//轮播图
#import "ZOrderMallBannerCell.h"
#import "ZOrderMallWebVC.h"
#import "ZOrderMallBannerModel.h"

#import "BOrderMallRootMenuCell.h"//功能菜单cell
#import "ZYHouBaoOrderFormViewController.h"

#import "BannerDetailsViewController.h" //轮播图详情
#import "BBuyNowShopCell.h"
#import "BShopPartModel.h"
#import "BBuyNowShopDetailVC.h"//产品详情

@interface AOrderMallRootVC ()
<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>
{
    BOOL _isRequesting;//是否正在请求数据
}
@property (nonatomic, strong) WMZBannerView *viewBanner;//轮播图
@property (nonatomic, strong) NSMutableArray *bannerListArray;//轮播图数据
@property (nonatomic, strong) UICollectionView *viewCollection;//块布局
@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *shopList;//产品列表
@property (nonatomic, assign) NSInteger pageNumber;

@end

@implementation AOrderMallRootVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    //状态栏字体白色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    //状态栏字体黑色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupNavViewAction];
    [self setupViewAction];
    
    [self requestBannerData];
    self.shopList = [NSMutableArray array];
}

#pragma mark - 自定义导航栏
- (void)setupNavViewAction{
    UIView *viewNav = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kNaviHeight)];
    [self.view addSubview:viewNav];
    
    UIImageView *ivBg = [[UIImageView alloc] initWithFrame:viewNav.bounds];
    ivBg.image = imgname(@"base_nav_bg");
    [viewNav addSubview:ivBg];
    
    UILabel *lblNav = [[UILabel alloc] initWithFrame:CGRectMake(0, kNaviHeight - 44, kScreen_Width, 44)];
    lblNav.text = @"产品展示";
    lblNav.font = [UIFont systemFontOfSize:18];
    lblNav.textColor = kColorWithHex(0xffffff);
    lblNav.textAlignment = NSTextAlignmentCenter;
    [viewNav addSubview:lblNav];
}
#pragma mark - 界面布局方法
- (void)setupViewAction{
    self.view.backgroundColor = kColorWithHex(0xf8f8f8);
    //新需求 暂时不显示轮播图
    //    [self setupBannerView];
    [self configCollectView];
    [self configBackTableView];
}
#pragma mark - 轮播图布局实现
//- (void)setupBannerView{
//    kWeakSelf;
//    WMZBannerParam *param = BannerParam()
//    .wMyCellClassNameSet(@"ZOrderMallBannerCell")
//    .wMyCellSet(^UICollectionViewCell *(NSIndexPath *indexPath, UICollectionView *collectionView, ZOrderMallBannerModel *model, UIImageView *bgImageView ,NSArray*dataArr) {
//        ZOrderMallBannerCell *cell = (ZOrderMallBannerCell *)[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZOrderMallBannerCell class]) forIndexPath:indexPath];
//        cell.model = model;
//        return cell;
//    })
//    .wFrameSet(CGRectMake(0, kNaviHeight, kScreen_Width, 178))
//    .wLineSpacingSet(15)
//    .wScaleSet(YES)
//    .wActiveDistanceSet(400)
//    .wScaleFactorSet(0.5)
//    .wItemSizeSet(CGSizeMake(kScreen_Width * 0.8, 146))
//    .wContentOffsetXSet(0.5)
//    .wSelectIndexSet(2)
//    .wRepeatSet(YES)
//    .wAutoScrollSecondSet(3)
//    .wAutoScrollSet(YES)
//    .wPositionSet(BannerCellPositionCenter)
//    .wBannerControlSelectColorSet(kColorWithHex(0xd0d0d0))
//    .wBannerControlColorSet(kColorWithHex(0x888888))
//    .wDataSet(self.bannerListArray)
//    //让第一个和最后一个居中 设置为size.width的一半
//    .wSectionInsetSet(UIEdgeInsetsMake(0,kScreen_Width * 0.4, 0, kScreen_Width * 0.4))
//    .wEventClickSet(^(ZOrderMallBannerModel *anyID, NSInteger index) {
//        [weakSelf bannerClickWithIndex:index];
//        KMyLog(@"省级 产品展示 轮播图点击事件 %ld", (long)index);
//
//        self.index = index;
//        ZOrderMallBannerModel *model = self.bannerListArray[self.index];
//        if (model.details.length == 0) {
//
//        } else {
//            BannerDetailsViewController *vc = [[BannerDetailsViewController alloc] init];
//            vc.hidesBottomBarWhenPushed = YES;
//            vc.bannerId = model.Id;
//            vc.titleStr = model.title;
//            vc.partsId = model.partsId;
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//    });
//    self.viewBanner = [[WMZBannerView alloc] initConfigureWithModel:param withView:self.view];
//}
#pragma mark - UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5;
}

#pragma mark - UICollectionViewDataSource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BOrderMallRootMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BOrderMallRootMenuCell class]) forIndexPath:indexPath];
    cell.cellIndexPathRow = indexPath.row;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        //我要预订
        BBuyNowVC *vc = [BBuyNowVC new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 1){
        
        ZYHouBaoOrderFormViewController *vc = [ZYHouBaoOrderFormViewController new];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.row == 2){
        
        ZGrantListVC *vc = [ZGrantListVC new];
        [self.navigationController pushViewController:vc animated:YES];
       
    }else if (indexPath.row == 3){
        //预订管理
        ZMyOrderManagerVC *vc = [ZMyOrderManagerVC new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - 数据请求方法
-(void)requestBannerData{
    kWeakSelf;
    //1听单页轮播2商城轮播 3市县级商城 4省产品展示
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@(4) forKey:@"type"];
    
    [NetWorkTool POST:Mbannerapi param:param success:^(id dic) {
        KMyLog(@"省级 经销商 产品展示 轮播图片");
        NSArray *bannerArray = [dic objectForKey:@"data"];
        [bannerArray enumerateObjectsUsingBlock:^(NSDictionary  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ZOrderMallBannerModel *model = [ZOrderMallBannerModel mj_objectWithKeyValues:obj];
            [weakSelf.bannerListArray addObject:model];
        }];
        [weakSelf.viewBanner updateUI];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}
#pragma mark - 轮播图点击事件交互
- (void)bannerClickWithIndex:(NSInteger)index{
//    ZOrderMallBannerModel *model = [self.bannerListArray objectAtIndexSafe:index];
//    ZOrderMallWebVC *vc = [ZOrderMallWebVC new];
//    vc.webUrl = [NSString stringWithFormat:@"%@%@",ORDER_MALL_BANNER_DETAIL,model.Id];
//    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 懒加载
- (NSMutableArray *)bannerListArray{
    if (!_bannerListArray) {
        _bannerListArray = [NSMutableArray array];
    }
    return _bannerListArray;
}
#pragma mark -tableView--------
-(void)configCollectView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
       //设置单元格大小
       layout.itemSize = CGSizeMake((kScreen_Width - 10) / 2.0, 122);
       //最小行间距(默认为10)
       layout.minimumLineSpacing = 0;
       //最小item间距（默认为10）
       layout.minimumInteritemSpacing = 0;
       //设置senction的内边距
       layout.sectionInset = UIEdgeInsetsMake(20, 5, 0, 5);
       //设置UICollectionView的滑动方向
       layout.scrollDirection = UICollectionViewScrollDirectionVertical;
       _viewCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 244+20) collectionViewLayout:layout];
       _viewCollection.backgroundColor = kColorWithHex(0xffffff);
       _viewCollection.delegate = self;
       _viewCollection.dataSource = self;
       [_viewCollection registerClass:[BOrderMallRootMenuCell class] forCellWithReuseIdentifier:NSStringFromClass([BOrderMallRootMenuCell class])];
      _viewCollection.scrollEnabled = NO;
}
- (void)configBackTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, kScreen_Width, kScreen_Height -kNaviHeight) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    self.tableView.tableHeaderView = _viewCollection;
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _tableView.rowHeight = UITableViewAutomaticDimension;
    _tableView.estimatedRowHeight = 300;
    
    [_tableView registerClass:[BBuyNowShopCell class] forCellReuseIdentifier:NSStringFromClass([BBuyNowShopCell class])];
    
    kWeakSelf;
    //下拉刷新
    MJRefreshNormalHeader *mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pageNumber = 1;
        [weakSelf requestData];
    }];
    mj_header.lastUpdatedTimeLabel.hidden = YES;
    _tableView.mj_header = mj_header;
    
    //上拉加载
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestData];
    }];
    
    [_tableView.mj_header beginRefreshing];
    
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.shopList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BBuyNowShopCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BBuyNowShopCell class]) forIndexPath:indexPath];
    BShopPartModel *model = [self.shopList objectAtIndexSafe:indexPath.row];
    cell.model = model;
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BShopPartModel *model = [self.shopList objectAtIndexSafe:indexPath.row];
    BBuyNowShopDetailVC *vc = [BBuyNowShopDetailVC new];
    vc.shopID = model.Id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 数据请求
- (void)requestData{
    if (_isRequesting) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        return;
    }
    _isRequesting = YES;
    [MBProgressHUD showHUDAddedTo:self.view msg:@"数据加载中" animated:YES];
    kWeakSelf;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObjectIfNotNil:@"1" forKey:@"type"];
    [param setObject:@(_pageNumber) forKey:@"pageNum"];
    [NetWorkTool POST:getPartsList param:param success:^(id dic) {
        [weakSelf requestResult];
        
        NSArray *dataArr = [dic objectForKeyNotNil:@"data"];
        [dataArr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            BShopPartModel *model = [BShopPartModel mj_objectWithKeyValues:obj];
            [weakSelf.shopList addObjectNotNil:model];
        }];
        [weakSelf.tableView reloadData];
        weakSelf.pageNumber++;
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [weakSelf requestResult];
    } fail:^(NSError *error) {
        [weakSelf requestResult];
    } needUser:YES];
}
- (void)requestResult{
    _isRequesting = NO;
    [_tableView.mj_header endRefreshing];
    [_tableView.mj_footer endRefreshing];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    if (self.pageNumber == 1) {
        [self.shopList removeAllObjects];
    }
}
@end
